package com.ww.yaodao.util;

import com.google.code.kaptcha.Constants;
import com.ww.yaodao.model.bo.UserDO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpSession;
@Slf4j
public class UserContext {
    private static final  String USER_IN_SESSION = "loginUser";
    /**
     * 得到session对象
     * @return
     */
    public static HttpSession getSession(){
        return ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest().getSession();
    }

    /**
     * 得到验证码
     * @return
     */
    public static String getVerifyCode(){
        return (String) getSession().getAttribute(Constants.KAPTCHA_SESSION_KEY);
    }

    /**
     * 设置用户
     * @param admin
     */
    public static void setUserInfo(UserDO admin){
        getSession().setAttribute(USER_IN_SESSION,admin);
    }

    /**
     * 得到session中的用户
     * @return
     */
    public static UserDO getAttribute(){
        return(UserDO) getSession().getAttribute(USER_IN_SESSION);
    }
}
